package com.teradata.fbj.controller;


import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.mail.internet.MimeMessage;

import org.apache.ibatis.jdbc.SQL;
import org.apache.tomcat.dbcp.dbcp.BasicDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.teradata.fbj.pojo.linkParams;
import com.teradata.fbj.service.IConnDataBaseService;
//import com.teradata.fbj.service.IConnDataBaseService;
import com.teradata.fbj.util.GetConnection;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

@Controller
@RequestMapping(value="/")
public class ConnDataBaseController {
	
		@Resource
		 private IConnDataBaseService connDataBaseService;
		@Autowired
		@Qualifier("mailSender")
		 private  JavaMailSender sender;//spring-mail.xml配好的邮箱服务器
		
		
		private final String FROM_MAIL = "fubjieTest@163.com";

		
//第一步 连上数据库，返回该数据库所有表
		
		@RequestMapping(value="/connParams.action",method=RequestMethod.GET)
		@ResponseBody
		public Map<String, Object> ConnDataBase(linkParams lp) throws Exception {
			String driverClass = lp.getDriverClass();
			String baseUrl = lp.getBaseUrl();
			String username = lp.getUsername();
			String password = lp.getPassword();
			List<String> tableList = connDataBaseService.getTableName(lp);
			Map<String, Object> resultMap  =  new HashMap<String, Object>();
			if(tableList.size() != 0){
				resultMap.put("success", "success");
				resultMap.put("tableName", tableList);
			}
			return resultMap;
		}
		
		@RequestMapping(value="/createTable.action",method=RequestMethod.POST)
		@ResponseBody
		public Map<String, Object> createTable(@RequestParam String tableSql ,linkParams lp) throws Exception{
			Map<String, Object> resultMap = new HashMap<String, Object>();
			Map<String, Object> table = new HashMap<String, Object>();
			int result = connDataBaseService.createModel(tableSql, lp);
			if(result == 0){
				resultMap.put("success", "success");
			}else {
				resultMap.put("success", "false");
			}
			return resultMap;
		}
		
		@RequestMapping(value="/sendEmail.action",method=RequestMethod.GET)
		@ResponseBody
		public String sendEmail(){
			String result = "";
			 try {
		    	 MimeMessage mailMessage = sender.createMimeMessage();
				 MimeMessageHelper messageHelper = new MimeMessageHelper(mailMessage,true,"utf-8");
				 messageHelper.setTo("fubjie@163.com");
				 messageHelper.setFrom(FROM_MAIL);
				 messageHelper.setSubject("rager that");
				 messageHelper.setText("大吉大利，今晚吃鸡！",true);
				 sender.send(mailMessage);
				 System.out.println("发送邮件成功！");
				 result = "1";
			} catch (Exception e) {
				e.printStackTrace();
				System.out.println("发送邮件失败！");
				result = "2";
			}
			return result;
		}
		
		
}
